<!DOCTYPE html>
<html>

<!--
Copyright 2011 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!--
Generates a Geo Chart using the AdSense Management API and the
Google Chart Tools.

Author silvano.luciani@gmail.com (Silvano Luciani)
-->

  <head>
    <title>Geo Chart Example</title>
    <script type="text/javascript" src="auth.js"></script>
    <script type="text/javascript" src="util.js"></script>
    <script type="text/javascript"
      src='https://www.google.com/jsapi?autoload={"modules":[{"name":"visualization","version":"1"}]}'>
    </script>
  </head>
  <body>
    <div id="vis_div" style="width: 600px; height: 400px;"></div>

    <!-- Add a button for the user to click to initiate auth sequence -->
    <button id="authorize-button" style="visibility: hidden">Authorize</button>

    <script type="text/javascript">
    // Now.
    var endDate = new Date();
    // Six months ago.
    var startDate = new Date(endDate.getTime() - 1000 * 60 * 60 * 24 * 180);

    // Gets and renders the data.
    function makeApiCall() {
      gapi.client.load('adsense', 'v1', function() {
        var request = gapi.client.adsense.reports.generate({
          'startDate': formatDate(startDate),
          'endDate': formatDate(endDate),
          'metric': ['PAGE_VIEWS'],
          'dimension': ['COUNTRY_NAME'],
          'sort': ['COUNTRY_NAME']
        });

        request.execute(function(resp) {
          var data = new google.visualization.DataTable();
          data.addColumn('string', 'Country');
          data.addColumn('string', 'Page Views');
          data.addRows(resp.rows);
          var view = new google.visualization.DataView(data);
          view.setColumns([
            0,
            {calc:pageViewsToNumber, type:'number', label:'Page Views'},
          ]);
          drawVisualization(view);
        });

      });

    }

    // Converts the page views field to numeric values.
    function pageViewsToNumber(dataTable, rowNum){
      return parseInt(dataTable.getValue(rowNum, 1));
    }

    // Draws the geo chart.
    function drawVisualization(view) {
      var wrapper = new google.visualization.ChartWrapper({
        chartType: 'GeoChart',
        dataTable: view,
        containerId: 'vis_div'
      });
      wrapper.draw();
    }
    </script>
    <script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
  </body>
</html>

